﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.ViewQuery;

public partial class sysadmin_PaperManager_UserList : PowerCreator.LMS.Web.Base.BasePage
{
    ManagerPaper objManagerPaper = new ManagerPaper();
    ManagerQuestion objManagerQuestion = new ManagerQuestion();
    ViewQuery objViewQuery = new ViewQuery();
    const string SEARCH_USERPAPERHISTORY_PAPER_TUSER = "select UserPaperHistory.UPHistoryID,UserPaperHistory.FKUPHistoryCourseID,UserPaperHistory.FKUPHistoryUserID, UserPaperHistory.FKUPHistoryProjectID,(select TrainproName from TTrainProject where TrainproID=UserPaperHistory.FKUPHistoryProjectID) as ProjectName,(select TrainclaName from TTrainClass where TTrainClass.TrainclaID=UserPaperHistory.FKUPHistoryClassID) as ClassName,UserPaperHistory.UPHistoryReadState,UserPaperHistory.UPHistoryReadUserID,UserPaperHistory.FKUPHistoryClassID,UserPaperHistory.FKUPHistoryPaperID,UserPaperHistory.UPHistoryStartTime,UserPaperHistory.UPHistorySubmitTime,UserPaperHistory.UPHistoryScore,Paper.PaperTestTime,TUser.UserName,UserPaperHistory.UPHistoryReadTime from UserPaperHistory,Paper,TUser where Paper.PaperID=UserPaperHistory.FKUPHistoryPaperID and TUser.UserID=UserPaperHistory.FKUPHistoryUserID {0}";
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!GetRole(Role.SearchUserScore))
        //{
        //    Script.ShowAndHistory("您没有用户成绩查询的权限!", -1);
        //    return;
        //}
        if (!IsPostBack)
        {
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);

            InitPage();
        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lab_UserList_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_UserName.Text = GetLanageValueByKey("lab_PaperUserList_UserName", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private string GetSearch()
    {
        string search = string.Format("and TUser.UserName like '%{0}%'", txtUserName.Text.Trim());
        string strSearch = string.Format(SEARCH_USERPAPERHISTORY_PAPER_TUSER, search);
        return strSearch.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objViewQuery.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderGV(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        dt = objViewQuery.Query(search, LMSPageSize, pageIndex);
        dt.Columns.Add("url");
        //dt.Columns.Add("ProjectName");d
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            //dt.Rows[i]["ProjectName"] = objManagerProjectInfo.Load(int.Parse(dt.Rows[0]["ProjectID"].ToString())).ProjectName;
            dt.Rows[i]["url"] = "PaperID=" + dt.Rows[i]["FKUPHistoryPaperID"].ToString()+ "&ProjectID=" + dt.Rows[i]["FKUPHistoryProjectID"].ToString() + "&ClassID=" + dt.Rows[i]["FKUPHistoryClassID"].ToString() + "&UserID=" + dt.Rows[i]["FKUPHistoryUserID"].ToString() + "&CourseID=" + dt.Rows[i]["FKUPHistoryCourseID"].ToString();
        }
        gvUserList.DataSource = dt;
        gvUserList.DataBind();
    }
    //private void GridBand(string username)
    //{
    //    DataTable dt = new DataTable();
    //    StringBuilder str = new StringBuilder();
    //    str.Append("select UserPaperHistoryInfo.UPHID as ID,UserPaperHistoryInfo.UserID, UserPaperHistoryInfo.ProjectID,UserPaperHistoryInfo.ClassID,UserPaperHistoryInfo.PaperID,UserPaperHistoryInfo.PaperStateDate,UserPaperHistoryInfo.SubmitDate,UserPaperHistoryInfo.PaperScore,PaperInfo.TestTime,SecurityUserInfo.UserName,SecurityUserInfo.RealName from UserPaperHistoryInfo,PaperInfo,SecurityUserInfo where PaperInfo.PaperID=UserPaperHistoryInfo.PaperID and SecurityUserInfo.UserID=UserPaperHistoryInfo.UserID ");
    //    if (!string.IsNullOrEmpty(username))
    //    {
    //        str.Append(" and UserName like '%" + username + "%'");
    //    }
    //    ViewState["queryString"] = str.ToString();

    //    dt= FPager.ExecutePager(Convert.ToInt32(ViewState["pageIndex"].ToString()), Convert.ToInt32(ViewState["pageSize"].ToString()), ViewState["showString"].ToString(), ViewState["queryString"].ToString(), ViewState["whereString"].ToString(), ViewState["orderString"].ToString(), ViewState["tabName"].ToString(), out pageCount, out recordCount, out dqpageCount);
    //    gvUserList.DataSource = dt;
    //    dt.Columns.Add("url");
    //    for (int i = 0; i < dt.Rows.Count; i++)
    //    {
    //        dt.Rows[i]["url"] = "PaperID=" + Base64.Encode(dt.Rows[i]["PaperID"].ToString()) + "&ProjectID=" + Base64.Encode(dt.Rows[i]["ProjectID"].ToString()) + "&ClassID=" + Base64.Encode(dt.Rows[i]["ClassID"].ToString())  + "&UserID=" + Base64.Encode(dt.Rows[i]["UserID"].ToString());
    //    }
    //    ViewState["pageCount"] = pageCount;
    //    ViewState["recordCount"] = recordCount;
    //    this.labRecordCount.Text = recordCount.ToString();
    //    this.labPageCount.Text = pageCount.ToString();
    //    this.lab_dqpageCount.Text = this.dqpageCount.ToString();
    //    gvUserList.DataBind();
    //    check();

    //}
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);
    }
    protected void gvUserList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btn_view")
        {
            string a = e.CommandArgument.ToString();
            
            Script.RedirectURL("ReadedPaper.aspx?"+e.CommandArgument.ToString());
        }
    }
    protected void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");
        } 

    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
}
